<template>
  <van-list
    v-model:loading="listLoading"
    :finished="listFinished"
    finished-text="没有更多了"
    @load="onLoad"
    :immediate-check="true"
    :offset="100"
  >
    <ul class="category-list">
      <li
        class="category-item"
        v-for="(showItem, index) in showList"
        :key="showItem.showId"
        :data-id="showItem.showId"
        @click="goToShowDetail(showItem.showId)"
      >
        <div class="left">
          <img :src="showItem.imgUrl" class="thumb" />
        </div>

        <div class="right">
          <div class="show-title">
            <div class="title">
              <span class="city">{{ showItem.showCity }}</span
              >{{ showItem.showName }}
            </div>
          </div>

          <div class="show-date">
            {{ showItem.showDate }}
          </div>

          <div class="show-venue">
            {{ showItem.venueName }}
          </div>
          <div class="show-tags">
            <template v-for="tag in showItem.tags" :key="tag.tagId">
              <van-tag round class="tag" @click="clickTag(tag)">#{{ tag.tagName }}</van-tag>
            </template>
          </div>

          <div class="show-price">
            <span class="prefix">{{ showItem.priceInfo?.prefix }}</span>
            {{ showItem.priceInfo?.yuanNum }}
            <span class="suffix">{{ showItem.priceInfo?.suffix }}</span>
          </div>
        </div>
      </li>
    </ul>
  </van-list>

  <template v-if="!hasMore">
    <div
      class="filter-conditions"
      v-if="store.beginDateTime || store.tag || store.showCityList.length > 0"
    >
      <div class="up">
        <div class="title">以上是符合条件的演出，修改可重新查询</div>
        <div class="reset" @click="restAll"><van-icon name="delete-o" /><span>清空</span></div>
      </div>
      <div class="down">
        <van-tag round class="tag" v-if="store.beginDateTime" @click="resetDate"
          ><span>{{ store.beginDateTime }}-{{ store.endDateTime }} </span><van-icon name="cross"
        /></van-tag>

        <van-tag round class="tag" v-if="store.tag?.tagName" @click="resetTag">
          <span>{{ store.tag?.tagName }} </span>
          <van-icon name="cross" />
        </van-tag>

        <van-tag round class="tag" v-if="store.showCityList?.length > 0" @click="resetCity"
          ><span> {{ store.showCityList }}</span> <van-icon name="cross"
        /></van-tag>
      </div>
    </div>
  </template>
</template>

<script setup>
import { ref, toRefs, watch } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
import { useStore } from '@/stores/index.js'
const store = useStore()

const listLoading = ref(false)
const listFinished = ref(false)

// 筛选后结果列表
// const resultList = computed(() => store.showList)

const emit = defineEmits(['loadMore', 'resetDate', 'resetTag', 'resetCity'])

const props = defineProps(['hasMore', 'showList', 'closeLoading'])
const { hasMore, showList, closeLoading } = toRefs(props)

// 监听 hasMore 变化来更新 listFinished
watch(hasMore, (newVal) => {
  listFinished.value = !newVal
})

watch(closeLoading, (newVal) => {
  if (!newVal) {
    listLoading.value = false
  }
})

const goToShowDetail = (id) => {
  router.push({ path: '/show-detail', query: { showId: id } })
}

// 加载更多
const onLoad = () => {
  // console.log('当前 hasMore 状态:', hasMore.value)
  // 检查是否还有更多数据
  if (!hasMore.value || hasMore.value === null || hasMore.value === undefined) {
    listFinished.value = true
    listLoading.value = false
    hasMore.value = false
    // console.log('没有更多数据，设置 listFinished 为 true')
    return
  }

  // 通知父组件继续加载下一页
  console.log('触发 loadMore 事件')
  emit('loadMore')
  // 设置加载状态为 true，表示正在加载
  listLoading.value = true
}

const clickTag = (tag) => {
  // console.log('tag:', tag)
  emit('changeTag', tag)
}

const resetDate = () => {
  emit('resetDate')
  store.setBeginDateTime(null)
  store.setEndDateTime(null)
  console.log('resetDate')
}

//清除筛选tag
const resetTag = () => {
  emit('resetTag')
  store.setTag(null, null)
  console.log('resetTag')
}

const resetCity = () => {
  emit('resetCity')
  store.setShowCityList(null)
  console.log('resetCity')
}

const restAll = () => {
  resetDate()
  resetTag()
  resetCity()
}

// 提供方法供父组件更新加载状态
defineExpose({
  setLoading: (status) => {
    // 添加边界检查
    if (listLoading.value !== undefined) {
      listLoading.value = status
    }
  },
  setFinished: (status) => {
    // 添加边界检查
    if (listFinished.value !== undefined) {
      listFinished.value = status
    }
  },
  reset: () => {
    if (listLoading.value !== undefined && listFinished.value !== undefined) {
      listLoading.value = false
      listFinished.value = false
    }
  },
})
</script>

<style scoped lang="scss">
.category-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 30px;
  padding: 20px 15px 0;

  .category-item {
    display: flex;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 15px;

    .left {
      width: 100px;
      height: 150px;
      overflow: hidden;
      flex-shrink: 0;

      .thumb {
        width: 100%;
        height: 100%;
        object-fit: cover;
        object-position: center;
        display: block;
        border-radius: 10px;
      }
    }

    .right {
      height: 150px;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: space-between;

      /* 占据剩余空间 */
      .show-title {
        display: flex;
        align-items: flex-start;
        justify-content: flex-start;

        .title {
          color: #000;
          font-size: 16px;
          font-weight: 700;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;
          text-overflow: ellipsis;
          word-wrap: break-word;
          word-break: break-word;

          .city {
            color: #000;
            font-size: 10px;
            font-weight: 600;
            border: 1px solid rgba(0, 0, 0, 0.25);
            margin-right: 5px;
            padding: 2px;
          }
        }
      }

      .show-date {
        font-size: 14px;
        font-weight: 500;
      }

      .show-venue {
        font-size: 14px;
        font-weight: 400;
      }

      .show-tags {
        display: flex;
        align-items: center;
        gap: 10px;

        .tag {
          flex: 0 0 auto;
          background: #f5f5f5;
          color: #323038;
          font-size: 10px;
          font-weight: 500;
          padding: 4px 6px;
        }
      }

      .show-price {
        color: #ff1d41;
        font-size: 20px;
        letter-spacing: -1px;

        .prefix {
          font-size: 10px;
        }

        .suffix {
          font-size: 10px;
        }
      }
    }
  }
}

.filter-conditions {
  background: #f6f6f7;
  padding: 15px 10px;
  margin-bottom: 70px;

  .up {
    display: flex;
    margin-bottom: 10px;
    align-items: center;
    justify-content: space-between;

    .title {
      font-size: 12px;
      margin-bottom: 10px;
    }

    .reset {
      display: flex;
      align-items: center;

      span {
        font-size: 12px;
      }
    }
  }

  .down {
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;

    .tag {
      padding: 6px 10px;
      gap: 5px;
      color: #323038;
      background-color: #fff;
    }
  }
}
</style>
